﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace inf_1007_devoir3
{
    class CtrlLocation
    {
        private static CtrlLocation instance;
        private List<Location> locations;

        private CtrlLocation()
        {
            locations = new List<Location>();
            initialiser();
        }

        public static CtrlLocation getInstance()
        {
            if (instance == null)
            {
                instance = new CtrlLocation();
            }

            return instance;
        }

        private void initialiser()
        { 
            locations.Add(new Location(CtrlClient.getInstance().getClients()[0], CtrlVehicule.getInstance().getVehicules()[0], new DateTime(2013,4,23), new DateTime(2013,4,30), 100000));
            locations.Add(new Location(CtrlClient.getInstance().getClients()[1], CtrlVehicule.getInstance().getVehicules()[3], new DateTime(2013, 4, 21), new DateTime(2013, 4, 25), 100000));
            locations.Add(new Location(CtrlClient.getInstance().getClients()[0], CtrlVehicule.getInstance().getVehicules()[2], new DateTime(2013, 4, 19), new DateTime(2013, 4, 30), 100000));
        }

        public void afficherListeLocations()
        {
            frmListeLocations frm = new frmListeLocations();

            frm.ShowDialog();
        }

        public void afficherNouvelleLocation(frmListeLocations frmListe)
        {
            frmLocation frm = new frmLocation(frmListe);

            frm.ShowDialog();
        }

        public void afficherLocation(frmListeLocations frmListe, Location location)
        {
            frmLocation frm = new frmLocation(frmListe, location);

            frm.ShowDialog();
        }

        public Location creerLocation(Client client, Vehicule vehicule, DateTime dateDebut, DateTime dateFin, int kilometrageDepart)
        {
            return new Location(client, vehicule, dateDebut, dateFin, kilometrageDepart);
        }

        public void ajouterLocation(Location location)
        {
            locations.Add(location);
        }

        public void passerAuPaiement(frmListeLocations frmListe, frmLocation frmLoc, Location loc)
        {
            frmLoc.Close();
            frmLoc.Dispose();

            frmPaiementLocation frm = new frmPaiementLocation(frmListe, loc);
            frm.ShowDialog();
        }


        //Fonction qui retourne la liste des locations selon plusieurs critères de recherche et d'ordonnancement
        public List<Location> getLocations(int ordre = 2, string[] recherche = null)
        {
            //Création de la nouvelle liste en excluant les locations qui ne répondent pas aux critères de recherche
            
            List<Location> arrayLocations = new List<Location>();

            for(int i = 0; i < locations.Count; i++)
            {
                bool ajout = true;

                if (recherche != null)
                {
                    if (recherche[0] != "" && !locations[i].getClient().ToString().Contains(recherche[0]))
                    {
                        ajout = false;
                    }

                    if (recherche[1] != "" && !locations[i].getVehicule().ToString().Contains(recherche[1]))
                    {
                        ajout = false;
                    }

                    if (recherche[2] != "" && !locations[i].getDateDebut().ToString().Contains(recherche[2]))
                    {
                        ajout = false;
                    }

                    if (recherche[3] != "" && !locations[i].getDateFin().ToString().Contains(recherche[3]))
                    {
                        ajout = false;
                    }

                    if (recherche[4] != "" && !locations[i].getDateRetour().ToString().Contains(recherche[4]))
                    {
                        ajout = false;
                    }
                }

                if (ajout)
                    arrayLocations.Add(locations[i]);
            }
            
            //Tri de la liste

            switch (ordre)
            { 
                case 0:
                    arrayLocations.Sort((x, y) => string.Compare(x.getClient().ToString(), y.getClient().ToString()));
                    break;
                case 1:
                    arrayLocations.Sort((x, y) => string.Compare(x.getVehicule().ToString(), y.getVehicule().ToString()));
                    break;
                case 2:
                    arrayLocations.Sort((x, y) => string.Compare(x.getDateDebut().ToString(), y.getDateDebut().ToString()));
                    break;
                case 3:
                    arrayLocations.Sort((x, y) => string.Compare(x.getDateFin().ToString(), y.getDateFin().ToString()));
                    break;
                case 4:
                    arrayLocations.Sort((x, y) => string.Compare(x.getDateRetour().ToString(), y.getDateRetour().ToString()));
                    break;
            }

            return arrayLocations;
        }

        public void supprimerLocation(Location location)
        {
            locations.Remove(location);
        }

        public bool existeDansListe(Location location)
        {
            return locations.Contains(location);
        }

        public void afficherRetour(frmListeLocations frmListeLoc, Location location)
        {
            frmRetourLocation frm = new frmRetourLocation(frmListeLoc, location);

            frm.ShowDialog();
        }

        public void passerAuPaiementRetour(frmListeLocations frmListe, frmRetourLocation frmRetour, Location location, double montant, string description, int kilometrage)
        {
            Location oldLoc = new Location();
            location.copier(oldLoc);

            location.retourner(montant, description, kilometrage);

            frmRetour.Close();

            frmPaiementRetour frm = new frmPaiementRetour(frmListe, location, oldLoc);

            frm.ShowDialog();
        }

        public void payer(Location loc, Location oldLoc, double montant, bool contratSigne)
        {
            loc.payer(montant, contratSigne, oldLoc);
        }

        public void payerRetour(Location location)
        {
            location.payerRetour();
        }
    }
}
